package com.synergy.stockflow.procure.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.entity.SysUser;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.procure.entity.PurchaseDetails;
import com.synergy.stockflow.procure.entity.PurchaseOrder;
import com.synergy.stockflow.procure.entity.SupplierPrices;
import com.synergy.stockflow.procure.entity.Vendor;
import com.synergy.stockflow.procure.service.AcceptanceOnArrivalService;
import com.synergy.stockflow.procure.service.PurchaseDetailsService;
import com.synergy.stockflow.procure.service.PurchaseOrderService;
import com.synergy.stockflow.procure.service.VendorService;
import com.synergy.stockflow.sales.entity.Quotation;
import com.synergy.stockflow.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/purchaseOrder")
public class PurchaseOrderController {
    @Autowired
    PurchaseOrderService purchaseOrderService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    PurchaseDetailsService purchaseDetailsService;
    @Autowired
    VendorService vendorService;
    @Autowired
    AcceptanceOnArrivalService acceptanceOnArrivalService;


    @GetMapping
    public Result<Page<PurchaseOrder>> getAllOrderReturns(
            @RequestParam(name = "purchaseOrderId", required = false) String purchaseOrderId,
            @RequestParam(name = "purchaseOrderName", required = false) String purchaseOrderName,
            @RequestParam(name = "purchaseOrderProductionPlanningId", required = false) String purchaseOrderProductionPlanningId,
            @RequestParam(name = "purchaseOrderDateOfSigning", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date purchaseOrderDateOfSigning,
            @RequestParam(name = "purchaseOrderDeliveryDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date purchaseOrderDeliveryDate,
            @RequestParam(name = "purchaseOrderEmployeeId", required = false) String purchaseOrderEmployeeId,
            @RequestParam(name = "purchaseOrderVendorId", required = false) String purchaseOrderVendorId,
            @RequestParam(name = "purchaseOrderInboundWarehouse", required = false) String purchaseOrderInboundWarehouse,
            @RequestParam(name = "purchaseOrderState", required = false) String purchaseOrderState
    ){
        Page<PurchaseOrder> page = new Page<>(1, 10000);
        page=purchaseOrderService.selectAll(page, purchaseOrderId, purchaseOrderName, purchaseOrderProductionPlanningId, purchaseOrderDateOfSigning, purchaseOrderDeliveryDate, purchaseOrderEmployeeId, purchaseOrderVendorId, purchaseOrderInboundWarehouse, purchaseOrderState);
        for(PurchaseOrder p:page.getRecords()){
            LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
            p.setNickname(sysUserService.getOne(queryWrapper.eq(SysUser::getId,p.getPurchaseOrderApprover())).getNickname());
            queryWrapper = new LambdaQueryWrapper<>();
            p.setNickname1(sysUserService.getOne(queryWrapper.eq(SysUser::getId,p.getPurchaseOrderEmployeeId())).getNickname());
        }
        return Result.success(page);
    }

    @PostMapping
    public int insert(@RequestBody PurchaseOrder purchaseOrder){
        purchaseOrder.setPurchaseOrderState("申请中");
        //查询供应商
        LambdaQueryWrapper<Vendor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Vendor::getVendorId, purchaseOrder.getPurchaseOrderVendorId());
        List<Vendor> list1 = vendorService.list(queryWrapper);
        //修改采购订单地址
        purchaseOrder.setPurchaseOrderAddress(list1.get(0).getVendorAddress());
        purchaseOrder.setPurchaseOrderFulladdress(list1.get(0).getVendorFulladdress());
        //添加
        purchaseOrderService.save(purchaseOrder);
        List<PurchaseOrder> list = purchaseOrderService.list();
        for (PurchaseDetails p : purchaseOrder.getPurchaseDetailsList()) {
            p.setPurchaseDetailsPurchaseOrderId(list.get(list.size()-1).getPurchaseOrderId());
            p.setPurchaseDetailsId("DDD-"+String.valueOf(new Date().getTime()));
            purchaseDetailsService.save(p);
        }
        return 1;
    }

    @PutMapping
    public int update(@RequestBody PurchaseOrder purchaseOrder){
        purchaseOrderService.updateById(purchaseOrder);
        for (PurchaseDetails p : purchaseOrder.getPurchaseDetailsList()) {
            purchaseDetailsService.updateById(p);
        }
        return 1;
    }

    @DeleteMapping
    public int delete(@RequestBody List<PurchaseOrder> purchaseOrders){
        for(PurchaseOrder p : purchaseOrders){
            purchaseOrderService.deleteIs(p.getPurchaseOrderId());
        }
        return 1;
    }
}
